<?php
/*
Plugin Name: dotnetblog
Plugin URI: http://www.hengha.info
Description: Crawl blogs from http://dotnet.blog.com
Version: 1.0
Author: tomcat
Author URI: http://www.hengha.info
*/

function prefix_activation()
{
    wp_schedule_event(time(), 'hourly', 'blog_hourly_event_hook');
}
register_activation_hook(__FILE__, 'prefix_activation');

function get_anonymous_user_id(){
    $userId = username_exists('unkown');
    if ($userId==null) {
        $userId = wp_insert_user(array(
            'user_login' => 'unkown',
            'user_email' => 'unkown@hengha.info',
            'nickname' => '匿名',
            'user_pass' => wp_generate_password(20),
            'role' => 'subscriber'
        ));
    }
    return $userId;
}
function get_user_id($user){
    if($user){
        $login = md5($user->login);
        $userId = username_exists($login);
        if ($userId==null) {
            $userId = wp_insert_user(array(
                'nickname' => $user->login,
                'user_login' => $login,
                'user_email' => $login . '@hengha.info',
                'user_pass' => wp_generate_password(20),
                'role' => 'subscriber'
            ));
            $avatar = 'http://tuicool.qiniudn.com/th.jpg';
            if ($user->icon) {
                $avatar = sprintf('http://pic.qiushibaike.com/system/avtnew/%d/%d/medium/%s',
                    (int)($item->user->id / 10000), $user->id, $user->icon);
            }
            update_user_meta($userId, 'item_id', $user->id);
            update_user_meta($userId, 'avatar', $avatar);
        }
        return $userId;
    }else{
        return get_anonymous_user_id();
    }
}
function handle_jokes($model){
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 1
    );
    foreach ($model->items as $item) {
        $userId=get_user_id($item->user);
        $args['name'] = $item->id;
        $the_post = get_posts($args);
        if (!$the_post) {
            $imgUrl = '';
            if ($item->image) {
                $imgUrl = sprintf('<img src="http://pic.qiushibaike.com/system/pictures/%d/%d/medium/%s" />', (int)($item->id / 10000), $item->id, $item->image);
            }
            $tags=array('糗事百科');
            if($item->image){
                $tags[]='有图';
            }else{
                $tags[]='文本'
            }
            // TODO: 增加分类
            $postId = wp_insert_post(array(
                'post_title' => $item->id,
                'post_name' => $item->id,
                'post_content' => $item->content . $imgUrl,
                'post_status' => 'publish',
                'post_author' => $userId,
                'post_format' => 'status',
                'tags_input' => implode(',', $tags)
            ));

            update_post_meta($postId, 'published_at', $item->published_at);
            update_post_meta($postId, 'created_at', $item->created_at);
        }
    }
}
function get_latest_blogs()
{
    $content = wp_remote_get('http://m2.qiushibaike.com/article/list/suggest?page=1&count=30',
        array('user-agent' => 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; LENW8)',
        'httpversion' => '1.1',
        'timeout' => 120));
    $model = json_decode($content['body']);
    handle_jokes($model);
}
add_action('blog_hourly_event_hook', 'get_latest_blogs');

function prefix_deactivation() {
    wp_clear_scheduled_hook( 'blog_hourly_event_hook');
}
register_deactivation_hook( __FILE__, 'prefix_deactivation' );
?>
